let app = getApp()
import {
  imageUrl
} from "../utils/imageutil"
import {
  rpxToPx
} from "../utils/util"
Page({

  data: {
    imageUrl,
    titleBarHeight: app.globalData.titleBarHeight,
    statusBarHeight: app.globalData.statusBarHeight,
    barUsableHeight: app.globalData.barUsableHeight,
    barUsableTop: app.globalData.barUsableTop,
    barUsableBottom: app.globalData.barUsableBottom,
    longitude: '',
    latitude: '',
    mapH: 0,
    list: [],
    formatted_address: '',
    id: '',
    car_info:null,
  },


  onLoad(options) {
    this.mapCtx = wx.createMapContext('mapId')
    this.getCity();

    if (app.globalData.isIphoneX) {
      this.setData({
        mapH: app.globalData.windowHeight - rpxToPx(366 + 68)
      })
    } else {
      this.setData({
        mapH: app.globalData.windowHeight - rpxToPx(366)
      })
    }
  },

  onShow() {
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1];
    if (currPage.data.id) {
      this.setData({
        id: currPage.data.id
      })
      this.user_vehicle_detail()
    }
  },

  user_vehicle_detail() {
    getApp().globalData.posts('/index.php/api/app/user/sorting/ouyoung_api/user_vehicle_detail?id=' + this.data.id).then(res => {
      if (res.data.code == 1) {
        let data = res.data.data
        this.setData({
          car_info: data
        })
      }else{
        this.setData({
          car_info: null
        })
      }
    })
  },

  getmy_address() {
    this.mapCtx.moveToLocation();
    this.getCity();
  },

  getCity() {
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        this.setData({
          longitude: Number(res.longitude),
          latitude: Number(res.latitude),
        })
        getApp().reverse_geocoding(res.latitude, res.longitude).then(r => {
          this.setData({
            formatted_address: r.formatted_address
          });
          this.updateLatAndLng(this.data.latitude, this.data.longitude, r.addressComponent.adcode).then(() => {
            this.housekeeperList()
          })
        })
      }
    })
  },

  updateLatAndLng(lat, lng, area_code) {
    return new Promise((reslove, reject) => {
      app.globalData.post({
        data: {
          do: 'login',
          openid: wx.getStorageSync('userInfo').openid,
          lat,
          lng,
          area_code
        }
      }).then(t => {
        wx.setStorageSync("userInfo", t.data);
        reslove()
      })
    })
  },


  onShareAppMessage() {
    let url='/xiche/index'
    url = encodeURIComponent(url)
    return{
      path:'/pages/index/blank?to_id='+wx.getStorageSync('userInfo').id+'&tourl='+url,
      title:'超有幸福',
    }
  },

  housekeeperList() {
    app.globalData.posts('/index.php/api/app/user/fjay').then(res => {
      // this.setData({
      //   list: res.data.data.list,
      // })
      this.bindEvent()
    })
  },

  bindEvent() {
    this.mapCtx.initMarkerCluster({
      enableDefaultStyle: false,
      zoomOnClick: true,
      gridSize: 60,
      complete(res) {
        console.log('initMarkerCluster', res)
      }
    })
    this.mapCtx.addMarkers({
      markers: [],
      clear: true
    })
    this.addMarkers()
  },
  addMarkers() {
    const marker = {
      width: 20,
      height: 20,
      borderRadius: 10,
      joinCluster: true,
      label: {
        borderRadius: 10,
        borderWidth: 0,
        borderColor: '#ffffff',
        // bgColor:'red',
        width: 20,
        height: 20,
        anchorX: -10,
        anchorY: -20
      }
    }
    const markers = []
    markers.push({
      width: 25,
      height: 25,
      iconPath:imageUrl+'/index/maps_red.png',
      joinCluster: false,
      latitude: this.data.latitude,
      longitude: this.data.longitude,
    })
    this.mapCtx.addMarkers({
      markers,
      clear: false,
      complete(res) {
        console.log('addMarkers', res)
      }
    })
    return
    this.data.list.forEach((p, i) => {
      const newMarker = Object.assign(marker, {})
      if (p.latitude == 0) {
        return
      }
      newMarker.id = i + 1;
      newMarker.iconPath = p.img;
      newMarker.latitude = p.lat;
      newMarker.longitude = p.lng;
      markers.push(newMarker)

      this.mapCtx.addMarkers({
        markers,
        clear: false,
        complete(res) {
          console.log('addMarkers', res)
        }
      })
    })
  },
  onMapTap(e) {
    this.setData({
      latitude: e.detail.latitude,
      longitude: e.detail.longitude,
    })
    getApp().reverse_geocoding(this.data.latitude, this.data.longitude).then(r => {
      this.setData({
        formatted_address: r.formatted_address
      });
    })
    this.bindEvent()
  },
  onMarkerTap(e) {
    console.log('@@ markertap', e)
  },
  onCalloutTap(e) {
    console.log('@@ onCalloutTap', e)
  },
  onLabelTap(e) {
    console.log('@@ labletap', e)
  },

  contact() {
    getApp().contact()
  },

  to_list(){
    wx.navigateTo({
      url: './list',
    })
  },

  openmap() {
    wx.chooseLocation({
      latitude: this.data.latitude,
      longitude: this.data.longitude,
      success: res => {
        console.log(res)
        this.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          formatted_address: res.address
        })
        this.bindEvent()
      }
    })
  },

  to_car() {
    wx.navigateTo({
      url: './car',
    })
  },

  to_good(){
    if(this.data.car_info){
      wx.navigateTo({
        url: './good?id='+this.data.car_info.id,
      })
    }
  },

})